package motor;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import lejos.nxt.comm.NXTConnection;
import lejos.nxt.comm.RS485;

public class barredora {
	private NXTConnection connector;
	private DataOutputStream dos;
	private DataInputStream dis;
	
	public barredora(){
//		this.connector = RS485.getConnector().connect("GRP062", NXTConnection.RAW);
//		this.dos = this.connector.openDataOutputStream();
//		this.dis = this.connector.openDataInputStream();
	}
	
	public void activar(){

		this.connector = RS485.getConnector().connect("GRP062", NXTConnection.RAW);
		this.dos = this.connector.openDataOutputStream();
		this.dis = this.connector.openDataInputStream();
		try {
			this.dos.writeInt(1);
			this.dos.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			this.dis.readInt();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	public void desactivar(){
		try {
			this.dos.writeInt(0);
			this.dos.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			this.dis.readInt();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			this.dos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			this.dis.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		this.connector.close();
		
	}

}
